home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 120_01.zip / CP.C < prev    next >
Text File  |  1993-06-01  |  3KB  |  143 lines

  1. /* HEADER: CUG120.05;
  2.    TITLE: CP;
  3.    DESCRIPTION: "File copy utility";
  4.    KEYWORDS: file copy;
  5.    SYSTEM: CP/M;
  6.    FILENAME: CP.C;
  7.    CRC: 9EA7;
  8.    SEE-ALSO: FPUT;
  9.    AUTHORS: Leor Zolman;
  10.    COMPILERS: BDS C;
  11. */
  12. /*
  13.     Makes a copy of a file in the current directory (easier
  14.     than doing a pip). Written by LZ. Uses the "usercode" library.
  15.  
  16.     Usage:
  17.         A>cp [user#/][d:]<filename> [user#/][d:][<filename>] <cr>
  18.  
  19.     Note that the second name is optional; if omitted, attempts to copy
  20.     to the given disk and/or user number. For example, to copy foo.c
  21.     into user area 7:
  22.         cp  foo.c  7/ 
  23.  
  24.     Link with:
  25.         A>clink cp -f usercode
  26. */
  27.  
  28. #define debug 0
  29. #include "bdscio.h"
  30.  
  31. main(argc,argv)
  32. char **argv;
  33. {
  34.     int i,j,c;
  35.     int fd1,fd2;
  36.     int bufsects;
  37.     unsigned bufsize, topofmem();
  38.     unsigned corebuf;
  39.     int orig_user;            /* original user area */
  40.     int source_user;        /* source user area */
  41.     int dest_user;            /* destination user area */
  42.     char destname[30];
  43.  
  44.  
  45.     if (argc != 3) {
  46.         printf("Usages: cp [u/]filename [u/]newname <cr>\n");
  47.         printf("    cp [u/]filename u/ <cr>\n");
  48.         exit();
  49.     }
  50.  
  51.                     /* get current user number */
  52.     source_user = dest_user = orig_user  = bdos(32,0xff);
  53.  
  54.     if ((fd1 = open(argv[1],0)) == ERROR) {
  55.         printf("Can't open %s\n",argv[1]);
  56.         exit();
  57.     }
  58.  
  59.     strcpy(destname,argv[2]);
  60.     if (hasuno(destname))
  61.         dest_user = atoi(destname);
  62.     if (hasuno(argv[1]))
  63.         source_user = atoi(argv[1]);
  64.  
  65.     if ( (c = destname[strlen(destname) - 1])=='/' || c == ':')
  66.     {
  67.         for (i = strlen(argv[1]) - 1; i >= 0; i--)
  68.             if (argv[1][i] == '/' || argv[1][i] == ':')
  69.                 break;                
  70.         strcat(destname,&argv[1][i+1]);
  71.     }
  72.  
  73.     if ((fd2 = creat(destname)) == ERROR) {
  74.       printf("Can't create %s\n",destname);
  75.         exit();
  76.     }
  77.  
  78.     corebuf = endext();
  79.     bufsize = topofmem() - 2500 - corebuf;
  80.     bufsects = bufsize / SECSIZ;
  81.  
  82. #if debug
  83.     printf("topofmem() = %x\n",topofmem());
  84.     printf("corebuf = %x\n",corebuf);
  85.     printf("topofmem() - 2500 - corebuf = %x, ",
  86.         topofmem() - 2500 - corebuf);
  87.     printf("bufsize = %x (%d decimal)\n",bufsize,bufsize);
  88.  
  89.     printf("bufsects = %d\n",bufsects);
  90.     printf("end of buffer = %x\n",corebuf+bufsize);
  91.     printf("&i = %x\n",&i);
  92. #endif
  93.  
  94.     printf("    copying...");
  95.  
  96.     while (1)
  97.     {
  98.         bdos(32,source_user);
  99.         if (kbhit()) getchar();
  100.         if (!(i = read(fd1,corebuf,bufsects))) break;
  101.         bdos(32,dest_user);
  102.         if (kbhit()) getchar();
  103.         if (write(fd2,corebuf,i) != i) {
  104.             printf("Write error. Disk full?\n");
  105.             exit();
  106.         }
  107.     }
  108.  
  109.     bdos(32,dest_user);
  110.     if (close(fd2) == ERROR) {
  111.         printf("Can't close the output file.\7\n");;
  112.     }
  113.     bdos(32,orig_user);
  114.     fabort(fd1);
  115.     printf("done.");
  116. }
  117.  
  118. /*
  119.     Return true if the string arg is a filename prefixed by "nn/",
  120.     where "nn" is a user number:
  121. */
  122.  
  123. int hasuno(str)
  124. char *str;
  125. {
  126.     char c;
  127.     int sum;
  128.  
  129.     sum = 0;
  130.  
  131.     if (!isdigit(*str)) return FALSE;
  132.  
  133.     while (isdigit(c = *str++))
  134.         sum = sum * 10 + c - '0';
  135.     return (c == '/') ? (sum >= 0 && sum < 32) : FALSE;
  136. }
  137.  
  138. /
  139.     char destname[30];
  140.  
  141.  
  142.     if (argc != 3) {
  143.         printf("Usages: cp [u/]fi